<!--
 * @Descripttion: 设置日志
 * @version:
 * @Author: 赵致喜
 * @Date: 2020-10-12 17:21:50
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-20 10:38:39
-->
<template>
  <div>
    <div class="control-box">
      <!-- control 操作区 START -->
      <e-control :search-form-options="formOptions"
                 :search-label-width="searchLabelWidth"
                 @on-search="searchControlHandle"
                 @on-refresh="refreshControlHandle">
      </e-control>
      <!-- control 操作区 END -->
    </div>
    <div class="table-box">
      <!-- table 数据表格 START -->
      <e-table :data="tableData"
               :columns="columns"
               stripe
               border
               :max-height="tableMaxHeight"
               style="width: 100%"
               :current="pageData.pageNum"
               :total="pageData.total"
               @page-size-change="changePageSize"
               @page-current-change="changePageCurrent">
        <template slot-scope="{scope}" slot="operation">
          <el-button @click="setPrice(scope.row)"
                     type="warning"
                     size="mini"
                     icon="el-icon-setting"
                     plain>设置</el-button>
        </template>
      </e-table>
      <!-- table 数据表格 END -->
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%">
      <span>按照当前价格设置规则，此商品自营价格将高于官网价，请确认是否以高于官网价的价格上架</span>
      <el-row style="margin-top: 20px;">
        <el-radio-group v-model="isAllowHigherOfficialPrice">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-row>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="setHigher">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>



<script>

import eControl from '@/components/e-control'
import formOptions from './control'
import {
  getQuerySelfOperatedPriceLog,
  allowedSelfExceedOffice,
  unAllowedSelfExceedOffice
} from '@/api/admin.api'
import columns from './columns'
export default {
  name: 'set-log',
  components: {
    eControl
  },
  data () {
    return {
      radio: 0,
      // 表格表头
      columns,
      // 表单配置项
      formOptions,
      // 查询表单
      searchFormData: {},
      // 搜索框label宽度
      searchLabelWidth: '100px',
      // 控制区高度
      controlHeight: 0,
      // 表格数据
      tableData: [],
      // 分页数据
      pageData: {
        // 当前分页
        pageNum: 1,
        // 分页条数
        pageSize: 10,
        // 数据总数
        total: 0
      },
      // 是否允许高于官网价
      isAllowHigherOfficialPrice: 0,
      dialogVisible: false,
      // 设置当前数据
      setCurrData: {}
    }
  },
  watch: {},
  computed: {
    //表格最大高度
    tableMaxHeight () {
      return window.innerHeight - this.controlHeight - 160
    }
  },
  methods: {
    /**
    * @functionName handleChange
    * @desccription 设置是否高于官网价
    * @author 穆宏宇
    * @date 2021-03-29 10:07:46
    * @version v1.0.0
    */
    handleChange() {
      const radio_tag = document.getElementsByName('price')
      for(let i=0;i<radio_tag.length;i++){
        if(radio_tag[i].checked){
          const checkvalue = radio_tag[i].value;
          this.isAllowHigherOfficialPrice = checkvalue;
        }
      }
    },
    // 页面初始化
    init () {
      this.getTableData()
    },
    /**
     * @functionName searchControlHandle
     * @param {Object} data 查询参数
     * @description 查询
     * @author 张航
     * @date 2020-09-08 15:01:26
     * @version V1.0.0
     */
    searchControlHandle (data) {
      this.searchFormData = data
      this.refreshPageCurrent()
    },
    /**
     * @functionName refreshControlHandle
     * @param {Object} data 重置后的数据
     * @description 重置查询条件
     * @author 张航
     * @date 2020-09-08 17:01:51
     * @version V1.0.0
     */
    refreshControlHandle (data) {
      this.searchFormData = data
    },
    /**
     * @functionName changePageSize
     * @param {Number} size 分页条数
     * @description 修改分页条数
     * @author 张航
     * @date 2020-09-08 17:57:06
     * @version V1.0.0
     */
    changePageSize (size) {
      this.pageData.size = size
      this.getTableData()
    },
    /**
     * @functionName changePageCurrent
     * @param {Number} current 当前分页
     * @description 修改当前分页
     * @author 张航
     * @date 2020-09-08 18:02:03
     * @version V1.0.0
     */
    changePageCurrent (current) {
      this.pageData.current = current
      this.getTableData()
    },
    /**
     * @functionName refreshPageCurrent
     * @description 重置当前分页
     * @author 张航
     * @date 2020-09-22 10:45:57
     * @version V1.0.0
     */
    refreshPageCurrent () {
      this.pageData.current = 1
      this.pageData.total = null
      this.getTableData()
    },
    // 获取日志列表
    getTableData () {
      const searchobj = { ...this.searchFormData, ...this.pageData }
      getQuerySelfOperatedPriceLog(searchobj).then(res => {
        this.tableData = res.records
        this.pageData.total = res.total
      })
    },
    // 设置高于官网价
    setHigher() {
      const {isAllowHigherOfficialPrice, setCurrData} = this;
      const params= {
        "sku": setCurrData.electronicCommerceCode,
        "supplierId": setCurrData.supplierId
      }
      if (isAllowHigherOfficialPrice) {
        // 添加商品高于官网价
        allowedSelfExceedOffice(params).then(()=>{
          this.dialogVisible = false;
          this.getTableData();
        })
      } else {
        // 移除商品可以高于官网价
        unAllowedSelfExceedOffice(params).then(()=>{
          this.dialogVisible = false;
          this.getTableData();
        })
      }
    },
    // 设置价格
    setPrice(row){
      this.dialogVisible = true;
      this.setCurrData = row;
      this.isAllowHigherOfficialPrice = row.isAllowHigherOfficialPrice === '1' ? 1 : 0;
    }
  },
  created () { },
  mounted () {
    this.init()
  }
}
</script>
<style lang="scss" scoped>
.control-box {
  margin-bottom: 10px;
}
</style>
